﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using DB.SchemaSync.Library.Interfaces;
using DB.SchemaSync.Library.Models;
using DB.SchemaSync.SqlServer;
using DBSync.Tools;
using Serilog;

namespace BDSync.Domains.DBSnapShot
{
    public class DBSnapShotServices : IDBSnapShotServices
    {
        public DBSnapShotServices()
        {
        }

        private String mDbConnStr;
        public String DbConnStr {
            get
            {
                return mDbConnStr;
            }
            set
            {
                mDbConnStr = value;
                DbProvider = new SqlServerDbProvider(mDbConnStr);
            }         
        }             
                      
        public IDbConnectionProvider DbProvider { get; private set; }

        public async Task<(Database db, string msg)> GetDataBaseRes()
        {
            try
            {
                if (!CheckConn())
                {
                    throw new ArgumentException("无法连接数据库");
                }

                var db = await DbProvider.GetDatabaseAsync();
                return (db, "");
            }
            catch (Exception e)
            {
                Log.Logger.Error(e, "扫描数据库失败");
                return (null, e.Message);
            }

            //Tuple<int, int> a = new Tuple<int,int>(1,1);
            //(int db , int  db2) b = (1,2) ;
        }

        public async Task<Database> GetDataBase()
        {
            try
            {
                if (!CheckConn())
                {
                    throw new ArgumentException("无法连接数据库");
                }

                return  await DbProvider.GetDatabaseAsync();
            }
            catch (Exception e)
            {
                Log.Logger.Error(e, "扫描数据库失败");
                throw e;
            }
            //Tuple<int, int> a = new Tuple<int,int>(1,1);
            //(int db , int  db2) b = (1,2) ;
        }


        /// <summary>
        /// 导出数据库快照
        /// </summary>
        /// <param name="folderpath"></param>
        /// <returns></returns>
        public async Task<string> ExportDbJson(string folderpath)
        {
            if (!CheckConn())
            {
                throw new ArgumentException("无法连接数据库");
            }

            using (var cn = DBFactory.GetCustomConn(DbConnStr))
            {
                var dbres = await DbProvider.GetDatabaseAsync();
                var exp = new DatabaseExport(dbres,cn);
                var res = await exp.GenJsonFile(folderpath);
                return res;
            }
        }


        public bool CheckConn()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(DbConnStr))
                {
                    throw new ArgumentException("连接参数为空");
                }

                using (var cn = DBFactory.GetCustomConn(DbConnStr))
                {
                    cn.Open();
                    return cn.State == ConnectionState.Open;
                }
            }
            catch (Exception e)
            {
                Log.Logger.Error(e,"数据库连接检查失败");
                return false;
            }
           
        }

        

    }
}
